package com.zzh.student.action;

import com.zzh.ssi.frame.base.BaseAction;
import com.zzh.student.dt.StudentDt;
import com.zzh.student.manager.StudentManagerImpl;

import java.util.List;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * Date: 13-1-10
 * Time: 下午4:22
 * To change this template use File | Settings | File Templates.
 */
public class StudentAction extends BaseAction {
    private StudentManagerImpl manager;
    private StudentDt dt;
    private StudentDt dt2;

    /**
     * 新增
     *
     * @return
     */
    public String add() {
        String target = "success";
        try {
            dt.setId(UUID.randomUUID().toString().replaceAll("-", ""));

            manager.add(dt);
            this.addMsg("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            this.addMsg("新增失败");
            return target;
        }
        return btnQuery();
    }

    /**
     * 新增（事务）
     *
     * @return
     */
    public String addByTran() {
        String target = "addByTran";
        try {
            dt.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            dt2.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            manager.addByTran(dt, dt2);
            this.addMsg("新增成功");

        } catch (Exception e) {
            e.printStackTrace();
            this.addMsg("新增失败");
            return target;
        }
        return btnQuery();
    }

    /**
     * 查询
     *
     * @return
     */
    public String btnQuery() {
        String target = "goQuery";
        try {
            List list = manager.query(dt, pageInfo);
            this.getRequest().setAttribute("list", list);
        } catch (Exception e) {
            e.printStackTrace();
            this.addMsg("查询失败");
            return target;
        }
        return target;
    }

    /**
     * 跳转到 更新页面
     *
     * @return
     */
    public String btnGoUpdate() {
        String target = "goUpdate";
        try {
            dt = manager.queryById(dt);
        } catch (Exception e) {
            log.error(e, e);
            this.addMsg("系统忙，请稍后再试！");
        }
        return target;

    }

    /**
     * 更新
     *
     * @return
     */
    public String btnUpdate() {
        String target = "goQuery";
        try {
            int res = manager.update(dt);
            if (res == 1) {
                this.addMsg("更新成功");
            } else {
                this.addMsg("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e, e);
            this.addMsg("更新失败");
        }
        return btnQuery();
    }

    /**
     * 删除
     *
     * @return
     */
    public String btnDelete() {
        String target = "goQuery";
        try {
            manager.delete(dt);
            this.addMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e, e);
            this.addMsg("删除失败");
        }
        dt.setName("");
        return btnQuery();
    }

    public StudentDt getDt() {
        return dt;
    }

    public void setDt(StudentDt dt) {
        this.dt = dt;
    }

    public StudentManagerImpl getManager() {
        return manager;
    }

    public void setManager(StudentManagerImpl manager) {
        this.manager = manager;
    }

    public StudentDt getDt2() {
        return dt2;
    }

    public void setDt2(StudentDt dt2) {
        this.dt2 = dt2;
    }
}
